package sv.gob.mh.dgii.common;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class SelectableItem<T> implements Serializable{

    private Object uniqueId;
    private boolean selected;
    private T element;

    public SelectableItem(T element) {
        uniqueId = (Math.random() * 1024 * Calendar.getInstance().getTimeInMillis());
        this.element = element;
    }

    public SelectableItem(Object uniqueId, T element) {
        this(element);
        this.uniqueId = uniqueId;
    }

    public boolean containedIn(List<SelectableItem<T>> list) {
        boolean ret = false;
        if (list != null && list.size() > 0) {
            for (SelectableItem<T> item : list) {
                if (this.equals(item)) {
                    ret = true;
                    break;
                }
            }
        }
        return ret;
    }

    public List<SelectableItem<T>> deleteFrom(List<SelectableItem<T>> list) {
        List<SelectableItem<T>> ret = new ArrayList<SelectableItem<T>>();
        List<SelectableItem<T>> removable = new ArrayList<SelectableItem<T>>();
        if (list != null && list.size() > 0) {
            for (SelectableItem<T> item : list) {
                if (this.equals(item)) {
                    removable.add(item);
                    break;
                }
            }
            list.removeAll(removable);
        }
        ret.addAll(list);
        return ret;
    }

    public List<SelectableItem<T>> addTo(List<SelectableItem<T>> list) {
        List<SelectableItem<T>> ret = new ArrayList<SelectableItem<T>>();
        list.add(this);
        ret.addAll(list);
        return ret;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 97 * hash + (this.uniqueId != null ? this.uniqueId.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final SelectableItem<T> other = (SelectableItem<T>) obj;
        if (this.uniqueId != other.uniqueId && (this.uniqueId == null || !this.uniqueId.equals(other.uniqueId))) {
            return false;
        }
        return true;
    }

    /**
     * @return the selected
     */
    public boolean isSelected() {
        return selected;
    }

    /**
     * @param selected the selected to set
     */
    public void setSelected(boolean selected) {
        this.selected = selected;
    }

    /**
     * @return the uniqueId
     */
    public Object getUniqueId() {
        return uniqueId;
    }

    /**
     * @param uniqueId the uniqueId to set
     */
    public void setUniqueId(Object uniqueId) {
        this.uniqueId = uniqueId;
    }

    /**
     * @return the element
     */
    public T getElement() {
        return element;
    }

    /**
     * @param element the element to set
     */
    public void setElement(T element) {
        this.element = element;
    }
}
